var utils = require("./utils");
var pointMap = {
  最近任务的关闭: [269, 1410, 451, 1592],
};
var appUtils = {
  openApp(appName) {
    var logger = utils.createLogger("appUtils openApp: ");
    var result = app.launchApp(appName);
    if (!result) {
      logger("打开应用-" + appName + "-失败，可能没有安装该应用");
      return false;
    }
    logger("打开-" + appName + "-成功");
    sleep(utils.getRandom(200, 300, 10));
    return true;
  },
  open(packageName) {
    var logger = utils.createLogger("appUtils open: ");
    var result = app.launch(packageName);
    if (!result) {
      logger("打开应用-" + packageName + "-失败，可能没有安装该应用");
      return false;
    }
    logger("打开-" + packageName + "-成功");
    sleep(utils.getRandom(200, 300, 10));
    return true;
  },
  close() {
    var logger = utils.createLogger("appUtils closeX: ");
    home();
    sleep(utils.getRandom(200, 220, 10));
    recents();
    sleep(utils.getRandom(200, 220, 10));
    var deviceProduct = device.product;
    if (deviceProduct == "cannon" || deviceProduct == "evergo") {
      swipe(
        (device.width / 2 / 4) * 3,
        device.height / 2,
        device.width / 20,
        device.height / 2,
        utils.getRandom(2, 4, 100)
      );
    } else {
      swipe(
        device.width / 2,
        device.height / 2,
        device.width / 2,
        device.height / 20,
        utils.getRandom(2, 4, 100)
      );
    }
    sleep(utils.getRandom(200, 220, 10));
    home();
    logger("关闭-" + "最近的应用" + "-成功");
    return true;
  },
  closeAll() {
    var logger = utils.createLogger("appUtils close: ");
    home();
    sleep(utils.getRandom(200, 220, 10));
    recents();
    sleep(utils.getRandom(200, 220, 10));
    utils.clickPoint.apply(
      utils,
      utils.getSizePoint(pointMap["最近任务的关闭"])
    );
    logger("关闭所有应用成功");
    return true;
  },
  ifInstalled(appName) {
    return !!app.getPackageName(appName);
  },
};
module.exports = appUtils;
